package Client;

import Server.Domain.Message;
import Server.Domain.MessageType;
import Utility.Scan;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

import static Client.ClientView.user;

public class ClientView2 {
    Message message =  new Message();
    Socket socket;
    ObjectOutputStream oos;
    ClientView2(Socket socket)  {
        this.socket = socket;
    }
    public void start() {
        //创建二级界面
        //创建子线程用于接受消息

        System.out.println("====" + user.getName() + ",欢迎!====");
        System.out.println("        1   好友列表       ");
        System.out.println("        2   在线好友       ");
        System.out.println("        3    私 聊           ");
        System.out.println("        4    群 聊           ");
        System.out.println("        5   传 文 件         ");
        System.out.println("        6   退   出       ");
        boolean loop2 = true;
        while (loop2) {
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("请输入对应序号:");
            String str1 = Scan.read();
            try {
                //如果每次创建的话: 头 - 对象
                oos = new ObjectOutputStream(socket.getOutputStream());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            switch (str1) {
                case "1":
                    message.setMessageType(MessageType.LIST);
                    try {
                        oos.writeObject(message);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                    break;

                case "2":
                    message.setMessageType(MessageType.ONLINE_LIST);
                    try {
                        oos.writeObject(message);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                    break;
                case "3":
                    message.setMessageType(MessageType.PRIVATE_CHAT);
                    message.setSender(user.getName());
                    System.out.print("请输入你要发消息的好友昵称");
                    message.setReceiver(Scan.read());
                    System.out.print("请输入消息:");
                    message.setContent(Scan.read());
                    try {
                        oos.writeObject(message);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
//                                            ois = new ObjectInputStream(socket.getInputStream());
//                                            message = (Message) ois.readObject();
//                                            System.out.println(message.getContent());
                    break;
                case "4":
                    message.setMessageType(MessageType.PUBLIC_CHAT);
                    message.setSender(user.getName());
                    System.out.print("请输入消息:");
                    message.setContent(Scan.read());
                    try {
                        oos.writeObject(message);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                    break;
                case "5":
                    System.out.println("list");
                    break;
                case "6":
                    message.setMessageType(MessageType.LOG_OFF);
                    try {
                        oos.writeObject(message);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                    loop2 = false;
                    break;
            }
        }
        try {
            socket.close();
            System.exit(0);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
